//
//  HotsiteListTableViewController.m
//  Interface
//
//  Created by Letme on 15-4-14.
//  Copyright (c) 2015年 zhenjiea. All rights reserved.
//

#import "HotsiteListTableViewController.h"

@interface HotsiteListTableViewController ()

@end

@implementation HotsiteListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
//循环 请求  不同city的 景点列表信息  
    NSString *url_string = self.cityUrl;
    
//    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/24059/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.88265156297574&longitude=121.53968615009308"];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        self.allModelArray = [NSMutableArray array];
        NSDictionary *dic = responseObject;
        NSArray *array = [dic objectForKey:@"items"];

        
        
// 将一个城市所有景点 请求
// 强制转换
        for (NSDictionary *smallDic in array) {
            HotsiteListModel *model = [[HotsiteListModel alloc] init];
            model.address = [NSString  stringWithFormat:@"%@", [smallDic objectForKey:@"address"]];
            model.arrival_type = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"arrival_type"]];
            model.cover_route_map_cover = [smallDic objectForKey:@"cover_route_map_cover"];
            model.description2 = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"description"]];
            model.fee = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"fee"]];
            model.name = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"name"]];
            model.name_en = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"name_en"]];
            model.opening_time = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"opening_time"]];
            model.recommended_reason = [NSString stringWithFormat:@"%@" ,[smallDic objectForKey:@"recommended_reason"]];
            model.visited_count = [NSString stringWithFormat:@"%@", [smallDic objectForKey:@"visited_count"]];
            model.wish_to_go_count = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"wish_to_go_count"]];
            
            
            model.url_id = [smallDic objectForKey:@"id"];
            
            [self.allModelArray addObject:model];
            
        }
        
        
        
#warning 
#warning  ?
    self.myTabelVeiw = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 80)];
    self.myTabelVeiw.delegate = self;
    self.myTabelVeiw.dataSource = self;
        //
    [self.view addSubview:self.myTabelVeiw];
        
        
        
       
    //    [self.myTabelVeiw reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    

    UIView *loadView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 10 + 10, self.view.frame.size.width, self.view.frame.size.height - 100)];
    loadView.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:loadView];
    UILabel *loadLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, loadView.frame.size.height / 2 - 25, self.view.frame.size.width, 50)];
    loadLabel.text = @"loading...";
    loadLabel.textAlignment =  NSTextAlignmentCenter;
    loadLabel.backgroundColor = [UIColor redColor];
    [loadView addSubview:loadLabel];
    [loadLabel release];
    [loadView release];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.allModelArray.count ;
    //return  self.view.frame.size.width / 3 + 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    HotsiteListModel *model = [self.allModelArray objectAtIndex:indexPath.section];
    cell.myModel = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  self.view.frame.size.width / 3 + 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HotsiteListModel *model = [[HotsiteListModel alloc] init];
    model = [self.allModelArray objectAtIndex:indexPath.section];
    SiteDetailPageViewController *siteDetailVC = [[SiteDetailPageViewController alloc] init];
    siteDetailVC.model = model;
    [self.navigationController pushViewController:siteDetailVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
